package com.hlkj.baojin.dao.repository;

import com.hlkj.baojin.constant.BaseRepository;
import com.hlkj.baojin.constant.BaojinConstant;
import com.hlkj.baojin.entity.Device;
import com.hlkj.baojin.entity.dto.DeviceSearchDto;
import com.hlkj.baojin.entity.dto.ModelList;
import com.hlkj.baojin.utils.PatternUtil;
import com.hlkj.baojin.utils.bean.Page;
import com.mongodb.client.result.UpdateResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author HULIN
 * @created with: baojin
 * @package com.hlkj.baojin.dao.repository
 * @description:
 * @date 2018/12/17 12:43
 * @modified By:
 * @Copyright © 2018 HAILIANG Info. Tech Ltd. All rights reserved.
 */
@Repository
public class DeviceRepository extends BaseRepository<Device> {

    public DeviceRepository(MongoTemplate mongoTemplate) {
        super(mongoTemplate);
    }

    public void updateDeviceNotNull(Device device) {
        Device deviceDB = new Device();
        deviceDB.setId(device.getId());
        super.updateByBean(deviceDB, device);
    }

    public ModelList<Device> getDevice(DeviceSearchDto deviceSearchDto) {

        Query query = new Query();

        if(StringUtils.isNotBlank(deviceSearchDto.getMultiCondition())) {
            query.addCriteria(Criteria.where("belongCompanyName").regex(PatternUtil.fuzzyMatch(deviceSearchDto.getMultiCondition())));
        }
        if(deviceSearchDto.getMonitorType() != null) {
            query.addCriteria(Criteria.where("monitorType").is(deviceSearchDto.getMonitorType()));

        }
        if(StringUtils.isNotBlank(deviceSearchDto.getDeptId())) {
            query.addCriteria(Criteria.where("belongCompany").is(deviceSearchDto.getDeptId()));

        }
        if(deviceSearchDto.getStatus() != null) {
            query.addCriteria(Criteria.where("status").is(deviceSearchDto.getStatus()));
        }
        query.addCriteria(Criteria.where("delete").is(BaojinConstant.IS_NO_DELETE));
        query.with(new Sort(Sort.Direction.DESC, "createTime"));
        query.with(new Page(deviceSearchDto.getPage(), deviceSearchDto.getSize()));

        List<Device> deviceList = mongoTemplate.find(query, Device.class);
        long count = mongoTemplate.count(query, Device.class);

        ModelList<Device> deviceModelList = new ModelList<>(deviceList, count);

        return deviceModelList;
    }

    public List<String> getIdsByNameAndItem(String search){
        Query query = new Query();
        if(search == null){
            search = "";
        }
        Pattern pattern = PatternUtil.fuzzyMatch(search);
        query.addCriteria(new Criteria().orOperator(
                Criteria.where("deviceName").regex(pattern),
                Criteria.where("monitorArticle").regex(pattern))
        );
        query.fields().include("id");

        List<Device> deviceList = mongoTemplate.find(query, Device.class);
        return deviceList.stream().map(Device::getId).collect(Collectors.toList());
    }

    public void deleteDevice(String deptId) {

        Query query = new Query();
        query.addCriteria(Criteria.where("delete").is(BaojinConstant.IS_NO_DELETE));
        query.addCriteria(Criteria.where("belongCompany").is(deptId));

        Update update = new Update();
        update.set("delete", BaojinConstant.IS_DELETE);

        mongoTemplate.updateMulti(query, update, Device.class);

    }
}
